# coding:utf8
import PIL
from PIL import ImageTk
from PIL import Image
import tkinter.messagebox
from tkinter import *
import configparser
import requests
from io import BytesIO


code = {
    '1' : '00000000',
    '2' : '00010000',
    '3' : '00020000',
    '4' : '00000001',
    '5' : '00010001',
    '6' : '00020001',
    '7' : '00000002',
    '8' : '00010002',
    '9' : '00020002',
}
send_url = 'https://tieba.baidu.com/cgi-bin/genimg?captchaservice633966397a4c31334b6a384248326d737157536848796a4736695055666143616145567542364b64533557484959653777576d54516463444a59326e6a50676741527133482b7768566948476555474d53376a4e6971654f6a7139516b4d34487733784530454553385662714f62637832664538796a7267686a6d30395539304c6b662f79332b63663142336238416f6472584833357a38666241384e7352585048583565324a7277585a666a574b4830455a5561766748706151334d526644313670414d57484b6d354a774c567865753954706844567761415a48455068475a7966454f6d54614936695a744b473672574a54626175632b6175342f337933484653487838316456533049714677364443643173523853376b672f6b622b342f5146654a72495a372b5a4642634959&tag=pc&t=0.15532535115546553'
cf = configparser.ConfigParser()
cf.read("post.conf")
cookie = cf.get("cookie", "cookie")
cookdic = dict(Cookie=cookie)


def text_wrapped_by(start, end ,content):
    si = content.find(start)
    if si != -1:
        si += len(start)
    ei = content.find(end, si)
    if ei != -1:
        return content[si:ei]
    return None


# 通过输入的位置来生成要发送过去的v_code代码
def gen():
    v_code.configure(state=NORMAL)
    v_code_str =''
    location_str = location.get()
    if location_str == "" or len(location_str) != 4:
        tkinter.messagebox.showinfo("Error", u"位置填写出错")
        return
    for i in range(4):
        v_code_str += code[location_str[i]]
    v_code.delete(0,tkinter.END)  # 清除文本框中的内容
    v_code.insert(0,v_code_str)  # 添加转换后的代码
    tkinter.messagebox.showinfo("success", u"转换成功")
    v_code.configure(state=DISABLED)


# 获取验证码图片并显示
def get_v_code_img():
    global image_file, im, image_label,url
    req = requests.get(send_url, cookies=cookdic, timeout=100)
    image_file = PIL.Image.open(BytesIO(req.content))
    new_img = image_file.resize((400, 400), PIL.Image.BILINEAR)
    im = ImageTk.PhotoImage(new_img)
    image_label.configure(image=im)
    image_label.pack(side="left")


# 进行验证
def post_v_code():
    check_url = 'https://tieba.baidu.com/f/commit/commonapi/checkVcode'
    captcha_input_str = v_code.get()
    captcha_vcode_str = text_wrapped_by("?", '&', send_url)  # 找到captchaservice那一串东西
    captcha_code_type = 4
    fid = 2936400
    post_args = {
        "captcha_vcode_str": captcha_vcode_str,
        "captcha_code_type": captcha_code_type,
        "captcha_input_str": captcha_input_str,
        "fid": fid,
    }
    ret = requests.post(check_url, cookies=cookdic, timeout=100, data=post_args)
    print(ret.text)  # 验证返回的结果

image_file = im = None

root = Tk()
root.title(u"验证码生成")
root.geometry("620x600")

var = StringVar()
l = Label(root, textvariable=var)
var.set(u"选择位置")
l.pack()
l.place(x=0, y=50)

var2 = StringVar()
v = Label(root, textvariable=var2)
var2.set(u"生成代码")
v.pack()
v.place(x=0, y=80)

location = Entry(root)
location.pack()
location.place(x=55, y=50)
v_code = Entry(root,width=40)
v_code.pack()
v_code.place(x=55, y=80)

but_gencode = Button(root, text=u"确认", height=1, width=10, command=gen)
but_gencode.pack()
but_gencode.place(x=380, y=50)

but_postcode = Button(root, text=u"发送", height=1, width=10, command=post_v_code)
but_postcode.pack()
but_postcode.place(x=380, y=80)

btn_get_v_code = Button(root, text=u"获取验证码", command=get_v_code_img)
btn_get_v_code.pack()
btn_get_v_code.place(x=0, y=10)

image_frame = Frame(root)
Label(root, text=u'验证码图片显示').place(x=0, y=135)
image_frame.pack()
image_frame.place(x=0, y=160)
image_label = Label(image_frame)

root.mainloop()
